//
// Created by ChenJi on 2021/4/17.
//

#include "interaction_handle.h"
#include "lcd_extern.h"
#include "sound.h"
#include "data_center.h"

typedef struct {
    uint8_t page_id;
    uint8_t active;
    uint8_t finger_sensor_active;
    uint8_t defeat_state;
    uint8_t isChenged;
} Main_Interface_Handle;

typedef struct {
    uint8_t page_id;
    uint8_t active;
    uint16_t now_page_id;
    uint16_t max_page;
    uint8_t isChenged;
    uint8_t page_updated;
} System_Log_Interface_Handle;
typedef struct {
    uint8_t pageid;
    uint8_t active;
    uint8_t isChenged;
    uint8_t page_updated;

} System_Setting_Interface;

typedef struct {
    uint8_t pageid;
    uint8_t active;
    uint8_t isChenged;
    uint8_t page_updated;

} Sensor_State_Interface;

Main_Interface_Handle main_interface_handle;
System_Log_Interface_Handle system_log_interface_handle;
System_Setting_Interface system_setting_interface;
Sensor_State_Interface sensor_interface;
uint8_t now_ID;


void active_Sensor_State_Interface_handle() {
    sensor_interface.active = 1;
    sensor_interface.pageid = SENSOR_STATE_ID;
    setLCDButtonLabel(1, 10);
    setLCDButtonLabel(2, 11);
    setLCDButtonLabel(3, 11);
    setLCDButtonLabel(4, 11);
    now_ID = SENSOR_STATE_ID;
    enableSensorStateHandle();
    sensor_interface.isChenged = 1;
}

void negative_Sensor_State_Interface_handle() {
    sensor_interface.active = 0;
    sensor_interface.isChenged = 0;
    if (now_ID == SENSOR_STATE_ID) {
        now_ID = 0;
    }
    disableSensorStateHandle();
}


void EX_Interact_Handle_Init() {
    active_Main_Interface_handle();
}


void Interact_Handle_Main_Thread_Execute() {
    execute_Main_Interface_handle();
    execute_LoggerCutdownPage();
    LCD_Handle_Main_Thread_Execute();

}

void active_System_setting_Interface_handle() {
    system_setting_interface.active = 1;
    system_setting_interface.pageid = SYSTEM_SETTING_ID;
    setLCDButtonLabel(1, 10);
    setLCDButtonLabel(2, 11);
    setLCDButtonLabel(3, 11);
    setLCDButtonLabel(4, 11);
    now_ID = SYSTEM_SETTING_ID;
    enable_setting_handle();
    system_setting_interface.isChenged = 1;
}

void negative_System_setting_interface_handle() {
    system_setting_interface.active = 0;
    system_setting_interface.isChenged = 0;
    if (now_ID == SYSTEM_SETTING_ID) {
        now_ID = 0;
    }
    disable_setting_handle();
}

void active_Main_Interface_handle() {
    reloadMainInterfaceEvent(&main_interface_handle.active);
    main_interface_handle.page_id = MAIN_INTERFACE_ID;
    chenge_Defeat_state(main_interface_handle.defeat_state);
    setLCDStatePicture(main_interface_handle.active);
    setLCDButtonLabel(2, 1);
    setLCDButtonLabel(3, 2);
    setLCDButtonLabel(4, 5);
    now_ID = MAIN_INTERFACE_ID;
}

void chenge_Defeat_state(uint8_t defeat_state) {
    main_interface_handle.defeat_state = defeat_state;
    main_interface_handle.isChenged = 1;
    if (defeat_state)
        setLCDButtonLabel(1, 4);
    else
        setLCDButtonLabel(1, 3);
}

void negative_Main_interface_handle() {
    main_interface_handle.active = 0;
    main_interface_handle.isChenged = 0;
    clearDefeatStatePicture();
    if (now_ID == MAIN_INTERFACE_ID) {
        now_ID = 0;
    }
}

void execute_Main_Interface_handle() {
    if(!main_interface_handle.active)return;
    if (!main_interface_handle.isChenged)return;
    if (now_ID != MAIN_INTERFACE_ID)return;
    main_interface_handle.isChenged = 0;
    setLCDStatePicture(main_interface_handle.defeat_state);
}

void button0FreeCallback() {
    if (now_ID == MAIN_INTERFACE_ID) {
        disableFingerSensorDisplay();
        if (main_interface_handle.defeat_state) {
            onDefeat_Disable_Apply();
        } else {
            onDefeat_Enable_Apply();
        }
        return;
    }
    if (now_ID == SYSTEM_LOGGER_ID) {
        negativeSystemLogger();
        disableFingerSensorDisplay();
        active_Main_Interface_handle();
        return;
    }
    if (now_ID == SYSTEM_SETTING_ID) {
        negative_System_setting_interface_handle();
        active_Main_Interface_handle();
        return;
    }
    if (now_ID == SENSOR_STATE_ID) {
        negative_Sensor_State_Interface_handle();
        active_Main_Interface_handle();
        return;
    }
}

void button1FreeCallback() {
    if (now_ID == MAIN_INTERFACE_ID) {
        negative_Main_interface_handle();
        disableFingerSensorDisplay();
        activeSystemLogger();
        return;
    }
    if (now_ID == SYSTEM_LOGGER_ID) {
        systemLoggerCutdownPage();
        return;
    }

}

void button1HoldCycleCallback() {
    if (now_ID == SYSTEM_LOGGER_ID) {
        systemLoggerCutdownPage();
        return;
    }
}

void button3HoldCycleCallback() {
    if (now_ID == SYSTEM_LOGGER_ID) {
        systemLoggerAddpage();
        return;
    }
}

void button2FreeCallback() {
    if (now_ID == MAIN_INTERFACE_ID) {
        negative_Main_interface_handle();
        disableFingerSensorDisplay();
        active_Sensor_State_Interface_handle();    return;
    }
}

void button3FreeCallback() {
    if (now_ID == MAIN_INTERFACE_ID) {
        negative_Main_interface_handle();
        disableFingerSensorDisplay();
        active_System_setting_Interface_handle();    return;
    }
    if (now_ID == SYSTEM_LOGGER_ID) {
        systemLoggerAddpage();
        return;
    }

}

void activeSystemLogger() {
    now_ID = SYSTEM_LOGGER_ID;
    system_log_interface_handle.page_id = 1;
    system_log_interface_handle.page_id = SYSTEM_LOGGER_ID;
    enable_log_show_handle();
    system_log_interface_handle.max_page = getIndex() / 5 + 1;
    system_log_interface_handle.now_page_id = 1;
    system_log_interface_handle.page_updated = 1;
    setLCDButtonLabel(1, 10);
    setLCDButtonLabel(2, 7);
    setLCDButtonLabel(3, 11);
    setLCDButtonLabel(4, 8);
}

void negativeSystemLogger() {
    now_ID = SYSTEM_LOGGER_ID;
    system_log_interface_handle.active = 0;
    system_log_interface_handle.isChenged = 1;
    disable_log_show_handle();
}

void systemLoggerAddpage() {
    if (system_log_interface_handle.now_page_id != system_log_interface_handle.max_page) {
        system_log_interface_handle.page_updated = 1;
        system_log_interface_handle.now_page_id++;
    }
}

void systemLoggerCutdownPage() {
    if (system_log_interface_handle.now_page_id != 1) {
        system_log_interface_handle.page_updated = 1;
        system_log_interface_handle.now_page_id--;
    }

}

void execute_LoggerCutdownPage() {
    if (!system_log_interface_handle.page_updated)return;
    system_log_interface_handle.page_updated = 0;
    uint8_t startIndex = 5 * (system_log_interface_handle.now_page_id - 1);
    for (int i = 0; i <= 4; i++) {
        setLogShow(i, i + startIndex);
    }
}

